package com.project.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import com.opensymphony.xwork2.ActionContext;
import com.project.entity.*;
import com.project.service.*;
public class MidCheckAction {
	private String start,limit,jsonString;
	private String projectnumber; //项目编号
	private String projectname;
	private Integer actualresultsid;
	
	
	private	MidCheck	midCheckService;
	private ProjectMidCheckManage projectMidCheckManage;
	public ActualResults result;
	private ProjectMidCheck projectMidCheck;
	public	ActualResults	actualRresult;
	

	public Integer getActualresultsid() {
		return actualresultsid;
	}

	public void setActualresultsid(Integer actualresultsid) {
		this.actualresultsid = actualresultsid;
	}

	public ActualResults getActualRresult() {
		return actualRresult;
	}

	public void setActualRresult(ActualResults actualRresult) {
		this.actualRresult = actualRresult;
	}

	public String getProjectname() {
		return projectname;
	}

	public void setProjectname(String projectname) {
		this.projectname = projectname;
	}

	public ProjectMidCheck getProjectMidCheck() {
		return projectMidCheck;
	}

	public void setProjectMidCheck(ProjectMidCheck projectMidCheck) {
		this.projectMidCheck = projectMidCheck;
	}

	public ProjectMidCheckManage getProjectMidCheckManage() {
		return projectMidCheckManage;
	}

	public void setProjectMidCheckManage(ProjectMidCheckManage projectMidCheckManage) {
		this.projectMidCheckManage = projectMidCheckManage;
	}

	public void setResult(ActualResults result) {
		this.result = result;
	}

	public String getStart() {
		return start;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public String getLimit() {
		return limit;
	}

	public void setLimit(String limit) {
		this.limit = limit;
	}

	public String getJsonString() {
		return jsonString;
	}

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}

	public String getProjectnumber() {
		return projectnumber;
	}

	public void setProjectnumber(String projectnumber) {
		this.projectnumber = projectnumber;
	}

	public MidCheck getMidCheckService() {
		return midCheckService;
	}

	public void setMidCheckService(MidCheck midCheckService) {
		this.midCheckService = midCheckService;
	}
	
	void clearValue(){
		this.start = null;
		this.limit = null;
		this.projectnumber = null;
		this.projectname = null;
		this.actualresultsid=null;
	}
	
	/**
	 * 用户权限获取所有的result
	 * @return
	 */
	public String	getAllResult(){
		ActionContext context = ActionContext.getContext();	
		if(context.getSession().get("right")== null || !context.getSession().get("right").equals("user")){
			this.setJsonString("{success:false,totalCount:" + 0+ ",list:[]}");
			return "success";
		}
		String number = (String)context.getSession().get("number");
		Map<String,String> map = new HashMap<String,String>();
		map.put("start", start);
		map.put("limit", limit);
		map.put("projectnumber", number);
		if(projectname!=null && !projectname.equals(""))
			map.put("name", projectname);
		
		List<ActualResults> list  = this.midCheckService.getAllResult(map);
		int size = this.midCheckService.getResultCount(map);
		JSONArray jsonObject = JSONArray.fromObject(list);
		this.setJsonString("{success:true,totalCount:" + size+ ",list:" + jsonObject.toString() + "}");
		this.clearValue();
		return "success";
	}
	
	
	
	
	
	/**
	 * 获取指定项目编号的成果信息
	 * @return
	 */
	public String getResult(){
		ActionContext context = ActionContext.getContext();	
		if(context.getSession().get("right")== null || !context.getSession().get("right").equals("user")){
			this.setJsonString("{success:false,totalCount:" + 0+ ",list:[]}");
			return "success";
		}
		String number = (String)context.getSession().get("number");
		Map<String,String> map = new HashMap<String,String>();
		map.put("projectnumber", number);
		ActualResults result = this.midCheckService.getResult(map);
		JSONArray jsonObject = JSONArray.fromObject(result	);
		this.setJsonString("{success:true,totalCount:" + 1+ ",list:" + jsonObject.toString() + "}");
		this.clearValue();
		return "success";
	}
	
	
	
	/**
	 * 管理员权限获取其所跟进项目编号的成果信息
	 * @return
	 */
	public String getFollowResult(){
		ActionContext context = ActionContext.getContext();	
		if(context.getSession().get("right")== null || !context.getSession().get("right").equals("manager")){
			this.setJsonString("{success:false,totalCount:" + 0+ ",list:[]}");
			return "success";
		}
		Integer FollowUpId =(Integer)context.getSession().get("id");
		List<ActualResults> result = this.projectMidCheckManage.getByFollowUpId(FollowUpId, start, limit);
		JSONArray jsonObject = JSONArray.fromObject(result	);
		this.setJsonString("{success:true,totalCount:" + result.size()+ ",list:" + jsonObject.toString() + "}");
		this.clearValue();
		return "success";
	}
	
	
	
	/**
	 * 管理员执行审批操作
	 * @return
	 */
	public String adminCheck(){
		ActionContext context = ActionContext.getContext();	
		if(context.getSession().get("right")== null || !(context.getSession().get("right").equals("manager")||context.getSession().get("right").equals("super"))){
			this.setJsonString("{success:false,totalCount:" + 0+ ",list:[]}");
			return "success";
		}
		this.projectMidCheckManage.adminMidCheck(this.actualresultsid);
		
		JSONArray jsonObject = JSONArray.fromObject(projectMidCheck	);
		this.setJsonString("{success:true,totalCount:" + 1+ ",list:" + jsonObject.toString() + "}");
		this.clearValue();
		return "success";
	}
	
	
	
	/**
	 * 获取没有被审批的结果
	 * @return
	 */
	public String getResultUncheck(){

		List<ActualResults> result = this.projectMidCheckManage.getAllUncheck(Integer.valueOf(start),Integer.valueOf(limit));
		JSONArray jsonObject = JSONArray.fromObject(result);
		this.setJsonString("{success:true,totalCount:" + 1+ ",list:" + jsonObject.toString() + "}");
		this.clearValue();
		return "success";
	}
	
	
	
	/**
	 * 更新result
	 */
	public String updateResult(){
		
		
		if (this.actualRresult != null && this.actualRresult.getActualresultsid() != null){
			
			ActualResults ar = this.midCheckService.getById(this.actualRresult.getActualresultsid());
			if(ar.getAuditmark().equals("1")){
				this.setJsonString("{success:false,totalCount:" + -3+ ",mark:-3}");
				return "success";
			}
			int i ;
			i = this.midCheckService.updateResult(actualRresult);
			if(i == -1)
				this.setJsonString("{success:false,totalCount:" + i+ ",list:{}}");
			else
				this.setJsonString("{success:true,totalCount:" + i+ ",list:{}}");
		}else
			this.setJsonString("{success:false,totalCount:" + -2+ ",list:{}}");
		this.clearValue();
		return "success";
	}
	
	
}
